package cn.xx.basic.controller;

import cn.xx.basic.util.AjaxResult;
import cn.xx.basic.util.FastDfsApiOpr;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/dfs")
public class FastDfsController {

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        try {
            //1.jpg
            String filename = file.getOriginalFilename();
            //获取后缀名
            String exeName = FilenameUtils.getExtension(filename);
            //上传附件，得到附件的路径
            String uploadPath = FastDfsApiOpr.upload(file.getBytes(), exeName);
            return AjaxResult.me().setResultObject(uploadPath);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());

        }
    }

    @DeleteMapping
    public AjaxResult del(@RequestParam(required = true,value = "path") String path){
        String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
        System.out.println(groupName);
        System.out.println(remotePath);
        FastDfsApiOpr.delete(groupName, remotePath);
        return  AjaxResult.me();
    }
}

